<?php
/**
 * A simple description for this script
 *
 * PHP Version 5.2.0 or Upper version
 *
 * @package    Paylog
 * @author     Hidehito NOZAWA aka Suin <http://suin.asia>
 * @copyright  2010 Hidehito NOZAWA
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2
 *
 */

class Paylog_Controller_DeleteTag extends Paylog_Abstract_Controller
{
	protected $tagHand   = null;
	protected $tagObj    = null;
	protected $tagId     = null;
	protected $errors    = array();

	public function __construct()
	{
		parent::__construct();

		$this->tagHand   = new Paylog_Object_TagHandler;
		$this->tagId     = intval(Paylog::get('id'));
		$this->tagObj    = $this->tagHand->load($this->tagId);
	}

	public function main()
	{
		$this->_validateAccess();

		if ( Paylog::post('delete') )
		{
			try
			{
				$this->_delete  ();
			}
			catch ( Exception $e )
			{
				$this->_default();
			}
		}
		else
		{
			$this->_default();
		}
	}

	protected function _delete()
	{
		$token = Paylog::post('token');

		if ( !Paylog_Class_Ticket::check($token) )
		{
			$this->errors[] = Paylog::msg("Ticket Error");
			throw new Exception();
		}


		if ( $this->tagHand->delete($this->tagId) )
		{
			$groupId = $this->tagObj->getVar('group_id');
			$extra = array('group_id' => $groupId);
			Paylog::redirect("Tags successly deleted.", Paylog::$dirname, 'tags', null, $extra);
		}
		else
		{
			$this->errors[] = Paylog::msg("Tags deleting failed.");
			throw new Exception();
		}
	}

	protected function _default()
	{
		$this->output['errors'] = $this->errors;
		$this->output['token']  = Paylog_Class_Ticket::issue();

		$this->output['tag'] = array(
			'id'     => $this->tagObj->getVar('id'),
			'name'   => $this->tagObj->getVar('name'),
			'weight' => $this->tagObj->getVar('weight'),
		);

		$this->_view();
	}

	protected function _validateAccess()
	{
		if ( $this->tagId < 1 )
		{
			Paylog::redirect("Tag ID is invalid.");
		}

		if ( !$this->tagHand->isIdExists($this->tagId) )
		{
			Paylog::redirect("Such a tag does not exists.");
		}
	}
}

?>
